package tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;


/**
 * Classe de racinisation (stemming) des mots
 * en fran�ais.
 * Modification l�g�re du package SnowBall
 * http://snowball.tartarus.org/download.php
 * @author xtannier
 *
 */
public class EnglishStemmer extends org.tartarus.snowball.ext.englishStemmer implements Normalizer {

	private static short REPEAT = 1;
	
	private ArrayList<String> stopWords;
	
	public EnglishStemmer() {
		this.stopWords = new ArrayList<String>();
	}
	
	public EnglishStemmer(String stopWordFileName) throws IOException {
		this.stopWords = new ArrayList<String>();
		//lecture du fichier texte	
		InputStream ips=new FileInputStream(stopWordFileName); 
		InputStreamReader ipsr=new InputStreamReader(ips);
		BufferedReader br=new BufferedReader(ipsr);
		String line;
		while ((line=br.readLine())!=null){
			this.stopWords.add(line);
		}
		br.close(); 
	}

	@Override
	public ArrayList<String> normalize(File file) throws IOException {
		return this.normalize(file, false);
	}
	@Override
	public ArrayList<String> normalize(File file, final boolean removeStopWords) throws IOException {		
		String text = "";
		//lecture du fichier texte	
		InputStream ips=new FileInputStream(file); 
		InputStreamReader ipsr=new InputStreamReader(ips);
		BufferedReader br=new BufferedReader(ipsr);
		String line;
		while ((line=br.readLine())!=null){
			text += line + " ";
		}
		br.close(); 
		return this.normalize(text, removeStopWords);
	}
	
	@Override
	public ArrayList<String> normalize(String text) throws IOException {
		return this.normalize(text, false);
	}

	@Override
	public ArrayList<String> normalize(String text, boolean removeStopWords)
			throws IOException {
		ArrayList<String> words = (new EnglishTokenizer()).tokenize(text.toLowerCase());
		ArrayList<String> result = new ArrayList<String>();
		for (String word : words) {
			// on ajoute le mot dans la liste s'il n'appartient pas � la liste des mots-cl�s.
			// Id�alement il faudrait utiliser une structure de donn�es plus efficace que la liste,
			// mais ce n'est pas le sujet.
			if (!removeStopWords || !(this.stopWords.contains(word) || word.length() == 1 || word.equals("..."))) {
				this.setCurrent(word);
				for (int i = REPEAT; i != 0; i--) {
					this.stem();
				}
				result.add(this.getCurrent());
			}
		}
		return result;
	}

	public static void main(String[] args) {
		String fileName = "/home/xtannier/data/PetitsCorpus/en-texte1.txt";
		EnglishStemmer stemmer = new EnglishStemmer();
		try {
			System.out.println(stemmer.normalize(fileName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}
